package interfaz;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.commons.model.UserException;

import tablero.Coordenada;
import tablero.Esfera;
import tablero.Tablero;

public class CrearEsferaDialog extends Dialog<Esfera> {
	Tablero currentMapa;

	public CrearEsferaDialog(WindowOwner owner, Esfera model,Tablero mapa, Coordenada coordenada) {
		super(owner, model);
		currentMapa = mapa;
	}

	@Override
	protected void createMainTemplate(Panel mainPanel) {
		this.setTitle("Creación de la esfera del dragón");
		super.createMainTemplate(mainPanel);
	}
	


	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel form = new Panel(mainPanel);
		form.setLayout(new ColumnLayout(2));

		new Label(form).setText("Número de estrellas");
		new TextBox(form).bindValueToProperty("numeroEstrellas").setAdapter(new NumeroEstrellasAdapter());

		new Label(form).setText("Ubicación");
		TextBox tx = new TextBox(form);
		tx.bindValueToProperty("coordenada").setAdapter(new CoodenadaAdapter());
		
	}
	
	@Override
	protected void addActions(Panel actions) {
		new Button(actions)
		.setCaption("Aceptar")
		.onClick(new MessageSend(this, ACCEPT))
		.setAsDefault()
		.disableOnError();

	new Button(actions) 
		.setCaption("Cancelar")
		.onClick(new MessageSend(this, CANCEL));
		
	}
	
	@Override
	protected void executeTask() {
		if(currentMapa.hayLugar(getModel().getCoordenada())){
			currentMapa.agregarEsfera(getModel());
		}
			else{
				throw new UserException("No se puede colocar a la esfera en esa ubicación");
			}
		}
		
	}
